home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / geninit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.5 KB  |  453 lines

  1. /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: geninit.c,v 1.3 2000/09/19 19:00:23 lpd Exp $ */
  20. /*
  21.  * Utility for merging all the Ghostscript initialization files (gs_*.ps)
  22.  * into a single file, optionally converting them to C data.  Usage:
  23.  *    geninit [-(I|i) <prefix>] <init-file.ps> <gconfig.h> <merged-init-file.ps>
  24.  *    geninit [-(I|i) <prefix>] <init-file.ps> <gconfig.h> -c <merged-init-file.c>
  25.  *
  26.  * The following special constructs are recognized in the input files:
  27.  *    %% Replace <#lines> (<psfile>)
  28.  *    %% Replace <#lines> INITFILES
  29.  * If the first non-comment, non-blank line in a file ends with the
  30.  * LanguageLevel 2 constructs << or <~, its section of the merged file
  31.  * will begin with
  32.  *    currentobjectformat 1 setobjectformat
  33.  * and end with
  34.  *    setobjectformat
  35.  * and if any line then ends with with <, the following ASCIIHex string
  36.  * will be converted to a binary token.
  37.  */
  38. #include "stdpre.h"
  39. #include <ctype.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>        /* for exit() */
  42. #include <string.h>
  43.  
  44. /* Forward references */
  45. private FILE *prefix_open(P2(const char *prefix, const char *inname));
  46. private void merge_to_c(P5(const char *prefix, const char *inname, FILE * in,
  47.                FILE * config, FILE * out));
  48. private void merge_to_ps(P5(const char *prefix, const char *inname, FILE * in,
  49.                 FILE * config, FILE * out));
  50.  
  51. #define LINE_SIZE 128
  52.  
  53. int
  54. main(int argc, char *argv[])
  55. {
  56.     int arg_c = argc;
  57.     char **arg_v = argv;
  58.     const char *fin;
  59.     FILE *in;
  60.     const char *fconfig;
  61.     FILE *config;
  62.     const char *fout;
  63.     FILE *out;
  64.     const char *prefix = "";
  65.     bool to_c = false;
  66.  
  67.     if (arg_c >= 2 && (!strcmp(arg_v[1], "-I") || !strcmp(arg_v[1], "-i"))) {
  68.     prefix = arg_v[2];
  69.     arg_c -= 2;
  70.     arg_v += 2;
  71.     }
  72.     if (arg_c == 4)
  73.     fin = arg_v[1], fconfig = arg_v[2], fout = arg_v[3];
  74.     else if (arg_c == 5 && !strcmp(arg_v[3], "-c"))
  75.     fin = arg_v[1], fconfig = arg_v[2], fout = arg_v[4], to_c = true;
  76.     else {
  77.     fprintf(stderr, "\
  78. Usage: geninit [-(I|i) lib/] gs_init.ps gconfig.h gs_xinit.ps\n\
  79. or     geninit [-(I|i) lib/] gs_init.ps gconfig.h -c gs_init.c\n");
  80.     exit(1);
  81.     }
  82.     in = prefix_open(prefix, fin);
  83.     if (in == 0) {
  84.     fprintf(stderr, "Cannot open %s for reading.\n", fin);
  85.     exit(1);
  86.     }
  87.     config = fopen(fconfig, "r");
  88.     if (config == 0) {
  89.     fprintf(stderr, "Cannot open %s for reading.\n", fconfig);
  90.     fclose(in);
  91.     exit(1);
  92.     }
  93.     out = fopen(fout, "w");
  94.     if (out == 0) {
  95.     fprintf(stderr, "Cannot open %s for writing.\n", fout);
  96.     fclose(config);
  97.     fclose(in);
  98.     exit(1);
  99.     }
  100.     if (to_c)
  101.     merge_to_c(prefix, fin, in, config, out);
  102.     else
  103.     merge_to_ps(prefix, fin, in, config, out);
  104.     fclose(out);
  105.     return 0;
  106. }
  107.  
  108. /* Open a file with a name prefix. */
  109. private FILE *
  110. prefix_open(const char *prefix, const char *inname)
  111. {
  112.     char fname[LINE_SIZE + 1];
  113.  
  114.     if (strlen(prefix) + strlen(inname) > LINE_SIZE) {
  115.     fprintf(stderr, "File name > %d characters, too long.\n",
  116.         LINE_SIZE);
  117.     exit(1);
  118.     }
  119.     strcpy(fname, prefix);
  120.     strcat(fname, inname);
  121.     return fopen(fname, "r");
  122. }
  123.  
  124. /* Read a line from the input. */
  125. private bool
  126. rl(FILE * in, char *str, int len)
  127. {
  128.     /*
  129.      * Unfortunately, we can't use fgets here, because the typical
  130.      * implementation only recognizes the EOL convention of the current
  131.      * platform.
  132.      */
  133.     int i = 0, c = getc(in);
  134.  
  135.     if (c < 0)
  136.     return false;
  137.     while (i < len - 1) {
  138.     if (c < 0 || c == 10)        /* ^J, Unix EOL */
  139.         break;
  140.     if (c == 13) {        /* ^M, Mac EOL */
  141.         c = getc(in);
  142.         if (c != 10 && c >= 0)    /* ^M^J, PC EOL */
  143.         ungetc(c, in);
  144.         break;
  145.     }
  146.     str[i++] = c;
  147.     c = getc(in);
  148.     }
  149.     str[i] = 0;
  150.     return true;
  151. }
  152.  
  153. /* Write a string or a line on the output. */
  154. private void
  155. wsc(FILE * out, const byte *str, int len)
  156. {
  157.     int n = 0;
  158.     const byte *p = str;
  159.     int i;
  160.  
  161.     for (i = 0; i < len; ++i) {
  162.     int c = str[i];
  163.  
  164.     fprintf(out,
  165.         (c < 32 || c >= 127 ? "%d," :
  166.          c == '\'' || c == '\\' ? "'\\%c'," : "'%c',"),
  167.         c);
  168.     if (++n == 15) {
  169.         fputs("\n", out);
  170.         n = 0;
  171.     }
  172.     }
  173.     if (n != 0)
  174.     fputc('\n', out);
  175. }
  176. private void
  177. ws(FILE * out, const byte *str, int len, bool to_c)
  178. {
  179.     if (to_c)
  180.     wsc(out, str, len);
  181.     else
  182.     fwrite(str, 1, len, out);
  183. }
  184. private void
  185. wl(FILE * out, const char *str, bool to_c)
  186. {
  187.     ws(out, (const byte *)str, strlen(str), to_c);
  188.     ws(out, (const byte *)"\n", 1, to_c);
  189. }
  190.  
  191. /*
  192.  * Strip whitespace and comments from a line of PostScript code as possible.
  193.  * Return a pointer to any string that remains, or NULL if none.
  194.  * Note that this may store into the string.
  195.  */
  196. private char *
  197. doit(char *line)
  198. {
  199.     char *str = line;
  200.     char *from;
  201.     char *to;
  202.     int in_string = 0;
  203.  
  204.     while (*str == ' ' || *str == '\t')        /* strip leading whitespace */
  205.     ++str;
  206.     if (*str == 0)        /* all whitespace */
  207.     return NULL;
  208.     if (!strncmp(str, "%END", 4))    /* keep these for .skipeof */
  209.     return str;
  210.     if (str[0] == '%')        /* comment line */
  211.     return NULL;
  212.     /*
  213.      * Copy the string over itself removing:
  214.      *  - All comments not within string literals;
  215.      *  - Whitespace adjacent to []{};
  216.      *  - Whitespace before /(<;
  217.      *  - Whitespace after )>.
  218.      */
  219.     for (to = from = str; (*to = *from) != 0; ++from, ++to) {
  220.     switch (*from) {
  221.         case '%':
  222.         if (!in_string)
  223.             break;
  224.         continue;
  225.         case ' ':
  226.         case '\t':
  227.         if (to > str && !in_string && strchr(" \t>[]{})", to[-1]))
  228.             --to;
  229.         continue;
  230.         case '(':
  231.         ++in_string;
  232.         case '<':
  233.         case '/':
  234.         case '[':
  235.         case ']':
  236.         case '{':
  237.         case '}':
  238.         if (to > str && !in_string && strchr(" \t", to[-1]))
  239.             *--to = *from;
  240.         continue;
  241.         case ')':
  242.         --in_string;
  243.         continue;
  244.         case '\\':
  245.         if (from[1] == '\\' || from[1] == '(' || from[1] == ')')
  246.             *++to = *++from;
  247.         continue;
  248.         default:
  249.         continue;
  250.     }
  251.     break;
  252.     }
  253.     /* Strip trailing whitespace. */
  254.     while (to > str && (to[-1] == ' ' || to[-1] == '\t'))
  255.     --to;
  256.     *to = 0;
  257.     return str;
  258. }
  259.  
  260. /* Copy a hex string to the output as a binary token. */
  261. private void
  262. hex_string_to_binary(FILE *out, FILE *in, bool to_c)
  263. {
  264. #define MAX_STR 0xffff    /* longest possible PostScript string token */
  265.     byte *strbuf = (byte *)malloc(MAX_STR);
  266.     byte *q = strbuf;
  267.     int c, digit;
  268.     bool which = false;
  269.     int len;
  270.     byte prefix[3];
  271.  
  272.     if (strbuf == 0) {
  273.     fprintf(stderr, "Unable to allocate string token buffer.\n");
  274.     exit(1);
  275.     }
  276.     while ((c = getc(in)) >= 0) {
  277.     if (isxdigit(c)) {
  278.         c -= (isdigit(c) ? '0' : islower(c) ? 'a' : 'A');
  279.         if ((which = !which)) {
  280.         if (q == strbuf + MAX_STR) {
  281.             fprintf(stderr, "Can't handle string token > %d bytes.\n",
  282.                 MAX_STR);
  283.             exit(1);
  284.         }
  285.         *q++ = c << 4;
  286.         } else
  287.         q[-1] += c;
  288.     } else if (isspace(c))
  289.         continue;
  290.     else if (c == '>')
  291.         break;
  292.     else
  293.         fprintf(stderr, "Unknown character in ASCIIHex string: %c\n", c);
  294.     }
  295.     len = q - strbuf;
  296.     if (len <= 255) {
  297.     prefix[0] = 142;
  298.     prefix[1] = (byte)len;
  299.     ws(out, prefix, 2, to_c);
  300.     } else {
  301.     prefix[0] = 143;
  302.     prefix[1] = (byte)(len >> 8);
  303.     prefix[2] = (byte)len;
  304.     ws(out, prefix, 3, to_c);
  305.     }
  306.     ws(out, strbuf, len, to_c);
  307.     free((char *)strbuf);
  308. #undef MAX_STR
  309. }
  310.  
  311. /* Merge a file from input to output. */
  312. private void
  313. flush_buf(FILE * out, char *buf, bool to_c)
  314. {
  315.     if (buf[0] != 0) {
  316.     wl(out, buf, to_c);
  317.     buf[0] = 0;
  318.     }
  319. }
  320. private void
  321. mergefile(const char *prefix, const char *inname, FILE * in, FILE * config,
  322.       FILE * out, bool to_c)
  323. {
  324.     char line[LINE_SIZE + 1];
  325.     char buf[LINE_SIZE + 1];
  326.     char *str;
  327.     int level = 1;
  328.     bool first = true;
  329.  
  330.     buf[0] = 0;
  331.     while (rl(in, line, LINE_SIZE)) {
  332.     char psname[LINE_SIZE + 1];
  333.     int nlines;
  334.  
  335.     if (!strncmp(line, "%% Replace ", 11) &&
  336.         sscanf(line + 11, "%d %s", &nlines, psname) == 2
  337.         ) {
  338.         flush_buf(out, buf, to_c);
  339.         while (nlines-- > 0)
  340.         rl(in, line, LINE_SIZE);
  341.         if (psname[0] == '(') {
  342.         FILE *ps;
  343.  
  344.         psname[strlen(psname) - 1] = 0;
  345.         ps = prefix_open(prefix, psname + 1);
  346.         if (ps == 0) {
  347.             fprintf(stderr, "Cannot open %s for reading.\n", psname + 1);
  348.             exit(1);
  349.         }
  350.         mergefile(prefix, psname + 1, ps, config, out, to_c);
  351.         } else if (!strcmp(psname, "INITFILES")) {
  352.         /*
  353.          * We don't want to bind config.h into geninit, so
  354.          * we parse it ourselves at execution time instead.
  355.          */
  356.         rewind(config);
  357.         while (rl(config, psname, LINE_SIZE))
  358.             if (!strncmp(psname, "psfile_(\"", 9)) {
  359.             FILE *ps;
  360.             char *quote = strchr(psname + 9, '"');
  361.  
  362.             *quote = 0;
  363.             ps = prefix_open(prefix, psname + 9);
  364.             if (ps == 0) {
  365.                 fprintf(stderr, "Cannot open %s for reading.\n", psname + 9);
  366.                 exit(1);
  367.             }
  368.             mergefile(prefix, psname + 9, ps, config, out, to_c);
  369.             }
  370.         } else {
  371.         fprintf(stderr, "Unknown %%%% Replace %d %s\n",
  372.             nlines, psname);
  373.         exit(1);
  374.         }
  375.     } else if (!strcmp(line, "currentfile closefile")) {
  376.         /* The rest of the file is debugging code, stop here. */
  377.         break;
  378.     } else {
  379.         int len;
  380.  
  381.         str = doit(line);
  382.         if (str == 0)
  383.         continue;
  384.         len = strlen(str);
  385.         if (first && len >= 2 && str[len - 2] == '<' &&
  386.         (str[len - 1] == '<' || str[len - 1] == '~')
  387.         ) {
  388.         wl(out, "currentobjectformat 1 setobjectformat\n", to_c);
  389.         level = 2;
  390.         }
  391.         if (level > 1 && len > 0 && str[len - 1] == '<' &&
  392.         (len < 2 || str[len - 2] != '<')
  393.         ) {
  394.         /* Convert a hex string to a binary token. */
  395.         flush_buf(out, buf, to_c);
  396.         str[len - 1] = 0;
  397.         wl(out, str, to_c);
  398.         hex_string_to_binary(out, in, to_c);
  399.         continue;
  400.         }
  401.         if (buf[0] != '%' &&    /* i.e. not special retained comment */
  402.         strlen(buf) + len < LINE_SIZE &&
  403.         (strchr("(/[]{}", str[0]) ||
  404.          (buf[0] != 0 && strchr(")[]{}", buf[strlen(buf) - 1])))
  405.         )
  406.         strcat(buf, str);
  407.         else {
  408.         flush_buf(out, buf, to_c);
  409.         strcpy(buf, str);
  410.         }
  411.         first = false;
  412.     }
  413.     }
  414.     flush_buf(out, buf, to_c);
  415.     if (level > 1)
  416.     wl(out, "setobjectformat", to_c);
  417.     fprintf(stderr, "%s: %ld bytes, output pos = %ld\n",
  418.         inname, ftell(in), ftell(out));
  419.     fclose(in);
  420. }
  421.  
  422. /* Merge and produce a C file. */
  423. private void
  424. merge_to_c(const char *prefix, const char *inname, FILE * in, FILE * config,
  425.        FILE * out)
  426. {
  427.     char line[LINE_SIZE + 1];
  428.  
  429.     fputs("/*\n", out);
  430.     while ((rl(in, line, LINE_SIZE), line[0]))
  431.     fprintf(out, "%s\n", line);
  432.     fputs("*/\n", out);
  433.     fputs("\n", out);
  434.     fputs("/* Pre-compiled interpreter initialization string. */\n", out);
  435.     fputs("\n", out);
  436.     fputs("const unsigned char gs_init_string[] = {\n", out);
  437.     mergefile(prefix, inname, in, config, out, true);
  438.     fputs("10};\n", out);
  439.     fputs("const unsigned int gs_init_string_sizeof = sizeof(gs_init_string);\n", out);
  440. }
  441.  
  442. /* Merge and produce a PostScript file. */
  443. private void
  444. merge_to_ps(const char *prefix, const char *inname, FILE * in, FILE * config,
  445.         FILE * out)
  446. {
  447.     char line[LINE_SIZE + 1];
  448.  
  449.     while ((rl(in, line, LINE_SIZE), line[0]))
  450.     fprintf(out, "%s\n", line);
  451.     mergefile(prefix, inname, in, config, out, false);
  452. }
  453.